import router from './router'
import { getLocalToken } from './utils/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import '@/styles/progress.scss'

//白名单
const whiteList = ['/login', '/404']
NProgress.configure({
  showSpinner: false,
})
//2.路由全局前置守卫
router.beforeEach((to, from, next) => {
  //开启进度条
  NProgress.start()
  //2.1获取token
  const token = getLocalToken()
  //2.2判断是否有token
  if (token) {
    //2.3有token
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
router.afterEach((to, from) => {
  NProgress.done()
  document.title = to.meta.title || '后台管理系统'
})
